package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.mapper.CategoryMapper;
import com.sky.properties.JwtProperties;
import com.sky.result.PageBean;
import com.sky.service.CategoryService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 分类业务层
 */
@Service
@Slf4j
@AllArgsConstructor
public class CategoryServiceImpl implements CategoryService {

    private final CategoryMapper categoryMapper;

    private final HttpServletRequest request;
    private final JwtProperties jwtProperties;

    /**
     * 修改分类
     *
     * @param categoryDTO 条件
     */
    @Override
    public void updateCategory(CategoryDTO categoryDTO) {
        categoryMapper.updateCategory(categoryDTO);
    }

    /**
     * 分页查询
     *
     * @param categoryPageQueryDTO 条件
     * @return PageBean
     */
    @Override
    public PageBean pageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageHelper.startPage(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize());
        Page page = categoryMapper.pageQuery(categoryPageQueryDTO);
        return new PageBean(page.getTotal(), page.getResult());
    }

    /**
     * 修改状态
     *
     * @param status   当前状态
     * @param category 分类id
     */
    @Override
    public void updateState(Integer status, Category category) {
        category.setStatus(status);
        categoryMapper.updateState(category);
    }

    /**
     * 添加分类
     *
     * @param category 添加信息
     */
    @Override
    public void addCategory(Category category) {
        //默认状态启动
        category.setStatus(0);
//        String token = request.getHeader(jwtProperties.getAdminTokenName());
//        Claims claims = JwtUtil.parseJWT(jwtProperties.getAdminSecretKey(), token);
//        Long aLong = Long.valueOf(claims.get(JwtClaimsConstant.EMP_ID).toString());
//        category.setCreateUser(aLong);
//        category.setUpdateUser(aLong);
        //把当前登录的用户id初始
//        category.setCreateUser(BaseContext.getCurrentId());
//        category.setUpdateUser(BaseContext.getCurrentId());

        categoryMapper.addCategory(category);
    }

    /**
     * 根据id删除
     *
     * @param category 实体对象
     */
    @Override
    public void deleteById(Category category) {
        categoryMapper.deleteById(category);
    }

    /**
     * 根据类型查询分类
     *
     * @param type 分类
     * @return list
     */
    @Override
    public List<Category> selectByType(Integer type) {
        List<Category> list = categoryMapper.selectByType(type);
        return list;
    }
}
